require "import"
import "android.provider.Settings"
import "android.content.Context"
import "android.widget.Toast"

-- Obtém o contexto do serviço
local context = service.getContext()
local resolver = context.getContentResolver()

-- Função para mostrar toast (que será lido pelo leitor de tela)
local function anunciar(texto)
    Toast.makeText(context, texto, Toast.LENGTH_SHORT).show()
end

-- Obtém o brilho atual
local brilhoAtual = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS)

-- Calcula a porcentagem atual
local porcentagemAtual = math.floor((brilhoAtual / 255) * 100)

-- Diminui o brilho em 50% (128 é metade de 255)
local novoBrilho = math.max(brilhoAtual - 128, 0)

-- Calcula a nova porcentagem
local novaPorcentagem = math.floor((novoBrilho / 255) * 100)

-- Aplica o novo brilho
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, novoBrilho)

-- Anuncia a mudança de brilho
local mensagem = string.format("Brilho reduzido para %d por cento", novaPorcentagem)
anunciar(mensagem)